home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Snippets / PPP_PrefSaver 1.1 / C Source / Fetch_Settings.c next >
Text File  |  1996-05-18  |  9KB  |  296 lines

  1. /* Fetch_Settings.c
  2.  
  3.     Simple dialog routines to handle receiving input of a user's account name
  4.     and password, which are then placed in the user's PPP connect settings,
  5.     used to log onto the service.
  6.     
  7.     The password and account name are entered into the connect script for the
  8.     active config.
  9.     
  10.     Correction in 1.0.1: The dialog item numbers for the account name and password
  11.     were corrected to the appropriate item numbers.  The numbers changed in the process
  12.     of editing the dialog and I didn't spot it.
  13.     
  14.     Changes in 1.0.1: The resources for the dialogs and alerts were changed to fit specifications
  15.     given in the InstallerMaker 3.0 users guide.
  16.     
  17.     Added in 1.1: Option to save account name/password into the Authentication settings
  18.     instead of in a connect script.  Also added #define NAME_COMMAND and PWORD_COMMAND
  19.     k-onstants to select fields of connect script to write to.
  20.     
  21.     Corrections in 1.1: SimpleModalFilter was fixed to correctly prevent users
  22.     from entering illegal characters.
  23.     Added check to make sure OK button is actually enabled before making a carriage
  24.     return into a hit on the OK button in SimpleModalFilter().
  25.     
  26.     See #define STANDALONE in the PPP_Prefs.h to change from compiling as a standalone
  27.     application to compiling as a code resource of type 'IEnd'.
  28. */
  29.  
  30. #include "PPP-Prefs.h"
  31.  
  32. #define        kMoveToFront        (WindowPtr)-1L
  33. #define        kDisable            255
  34. #define        kEnable                0
  35.  
  36. #define        kReturnKey            0x0D
  37. #define        kTildeKey            0x7E
  38. #define        kEnterKey            0x03
  39. #define        kESCKey                0x1B
  40.  
  41.  
  42. #define        kMaxCharLimit        8    // Maximum #chars allowed in name/password
  43.  
  44. /* THESE WERE REMOVED IN VERSION 1.0.2 FROM SimpleModalFilter() routine */
  45. //    #define        kPrintCharStart        0x20
  46. //    #define        kPrintCharEnd        0x7E
  47.  
  48. #define        kCtrlCharEnd        0x1F
  49. #define        kDeleteKey            0x7F
  50.  
  51.     // numeric chars top/bottom limits
  52. #define        kNumeralZero        0x30
  53. #define        kNumeralNine        0x39
  54.  
  55.     // Lowercase alphabetic chars top/bottom limits
  56. #define        kSmall_a            0x61
  57. #define        kSmall_z            0x7A
  58.     // Uppercase letters will be converted to lowercase
  59. #define        kBig_A                0x41
  60. #define        kBig_Z                0x5A
  61.  
  62. #define        kUpperToLowerCaseOffset    0x20
  63.  
  64. #define     kMyButtonDelay         8
  65. #define        kSimpleModalFilter    (ProcPtr)&SimpleModalFilter
  66.  
  67. #define        rUserPrefsDialog        3000
  68. #define            defOutline    3
  69. #define            iUserName    5
  70. #define            iPassword    6
  71.  
  72. #define        rConfirmSettingsAlrt    5001
  73.  
  74. /* Prototypes */
  75. Boolean            GetUserPrefs( struct ppp_config    *theConfig, OSErr *err );
  76. pascal Boolean    SimpleModalFilter( DialogPtr dialog, EventRecord *event,
  77.                                     short *itemHit );
  78. pascal void        buttonProc( DialogPtr theDialog, short theItem);
  79.  
  80.  
  81. /*************************** GetUserPrefs *********************************/
  82.  
  83. Boolean        GetUserPrefs( struct ppp_config    *theConfig, OSErr *err )
  84. {
  85.     DialogPtr        dialog;
  86.     GrafPtr            savePort;
  87.     Boolean            dialogDone = false, saveConfig = false;
  88.     short            iType, iHit;
  89.     Handle            iHandle, nameHandle, pwordHandle;
  90.     ControlHandle    okButton;
  91.     Rect            iRect;
  92.     Str255            theString, the_otherString;
  93.     Point            topCorner;
  94.     
  95.     GetPort(&savePort);
  96.  
  97.     dialog = GetNewDialog(rUserPrefsDialog, nil, kMoveToFront);
  98.     *err = LMGetResErr();
  99.     if (!dialog)    { goto getOut; }
  100.     
  101.     SetWRefCon(dialog, (long)GetUserPrefs);    
  102.     SetPort(dialog);
  103.         
  104.     GetDItem(dialog, defOutline, &iType, &iHandle, &iRect);
  105.     SetDItem(dialog, defOutline, iType, buttonProc, &iRect);
  106.     
  107.         // Disable "OK" until edit fields are filled in
  108.     GetDItem(dialog, ok, &iType, &okButton, &iRect);
  109.     HiliteControl(okButton, kDisable);
  110.     
  111.     GetDItem(dialog, iUserName, &iType, &nameHandle, &iRect);
  112.     GetDItem(dialog, iPassword, &iType, &pwordHandle, &iRect);
  113.     
  114.     ShowWindow(dialog);
  115.     
  116.     while (! dialogDone)
  117.     {
  118.         ModalDialog(kSimpleModalFilter, &iHit);
  119.         
  120.         switch( iHit)
  121.         {
  122.             case ok:
  123.                 HideWindow(dialog);
  124.                 GetIText(nameHandle, theString);
  125.                 GetIText(pwordHandle, the_otherString);
  126.                 ParamText(theString, the_otherString, 0,0);
  127.                 iHit = Alert(rConfirmSettingsAlrt,kSimpleModalFilter);
  128.                 if (iHit == ok)
  129.                     dialogDone = true;
  130.                 else
  131.                     ShowWindow(dialog);
  132.                 break;
  133.             case cancel:
  134.                 dialogDone = true;
  135.                 break;
  136.             default:
  137.                 GetIText(nameHandle, theString);
  138.                 if (theString[0])
  139.                     GetIText(pwordHandle, theString);
  140.                 
  141.                 if (! theString[0]){
  142.                     if ((*okButton)->contrlHilite == kEnable)
  143.                         HiliteControl(okButton, kDisable);
  144.                 }
  145.                 else{
  146.                     if ((*okButton)->contrlHilite == kDisable)
  147.                         HiliteControl(okButton, kEnable);
  148.                 }
  149.                 break;
  150.         }
  151.     }
  152.     
  153. #if ! DEMO_ONLY
  154.     if (iHit == ok)
  155.     {
  156.         /* Write the user's settings into the appropriate fields of the connect
  157.             script.  There are 8 fields total - theConfig->commands[0-7].scriptstr
  158.             (See NUMCOMMANDS in the PPP_Prefs.h) You select which field through
  159.             #defines in PPP_Prefs.h as NAME_COMMAND and PWORD_COMMAND
  160.         */
  161.         
  162.         GetIText(nameHandle, theString);
  163. #if USE_SCRIPT
  164.         BlockMove(theString, &(theConfig->commands[NAME_COMMAND].scriptstr), theString[0]+1);
  165. #else    // Write into Authentication settings instead
  166.         BlockMove(theString, &(theConfig->defaultid), theString[0]+1);
  167. #endif
  168.         
  169.         GetIText(pwordHandle, theString);
  170. #if USE_SCRIPT
  171.         BlockMove(theString, &(theConfig->commands[PWORD_COMMAND].scriptstr), theString[0]+1);
  172. #else    // Write into Authentication settings instead
  173.         BlockMove(theString, &(theConfig->defaultpw), theString[0]+1);
  174. #endif
  175.         saveConfig = true;
  176.     }
  177. #endif
  178.     
  179.     SetPort(savePort);
  180.     DisposDialog(dialog);
  181.     
  182. getOut:
  183.     return (saveConfig);
  184. }
  185.  
  186.     
  187. /************************* SimpleModalFilter ********************************/
  188.  
  189. pascal Boolean    SimpleModalFilter( DialogPtr dialog, EventRecord *event,
  190.                                     short *itemHit )
  191. {
  192.     Boolean         returnVal = false;
  193.     WindowPtr        temp;
  194.     ProcPtr            *standardProc;
  195.     short            iType, iHit;
  196.     Handle            iHandle;
  197.     Rect            iRect;
  198.     char            theChar;
  199.     Str255            iString;
  200.     long            waitTicks;
  201.             
  202.     /* This filter is used to queue the ok/cancel buttons via the keyboard
  203.         while remaining compatible with System 6.  It also limits the user
  204.         to entering no more than 8 alphanumeric characters in the edit text fields    */
  205.          
  206.     if((event->what == keyDown) || (event->what == autoKey))
  207.     {
  208.         theChar = (event->message & charCodeMask);
  209.         switch (theChar)
  210.         {
  211.             case kReturnKey:
  212.             case kEnterKey:
  213.                     /* change whatever is the current item to the OK item */
  214.                 GetDItem(dialog,OK,&iType,&iHandle,&iRect);
  215.                 if ((**(ControlHandle)iHandle).contrlHilite != kDisable){
  216.                     *itemHit = OK;
  217.                     HiliteControl((ControlHandle)iHandle,inButton);
  218.                     Delay(kMyButtonDelay,&waitTicks);    /* wait about 8 ticks */
  219.                     HiliteControl((ControlHandle)iHandle,false);
  220.                     returnVal = true;
  221.                 }
  222.                 else
  223.                     event->what = nullEvent;
  224.                 break;
  225.             case kESCKey:
  226.             case kTildeKey:
  227.                 *itemHit = cancel;
  228.                 GetDItem(dialog,cancel,&iType,&iHandle,&iRect);
  229.                 HiliteControl((ControlHandle)iHandle,inButton);
  230.                 Delay(kMyButtonDelay,&waitTicks);
  231.                 HiliteControl((ControlHandle)iHandle,false);
  232.                 returnVal = true;
  233.                 break;
  234.             default:
  235.                 if (GetWRefCon(dialog) == (long)GetUserPrefs)    // Ignore the confirm alert
  236.                 {
  237. /***** ERRRONEOUS CODE REMOVED: I WROTE THIS THINKING I COULD USE THE SAME PARAMETERS
  238.     USED BY the isprint() ROUTINE, HOWEVER THAT ALLOWS A LOT OF PRINTABLE CHARACTERS
  239.     TO SLIP BY THIS ROUTINE, SO I REWROTE THE CODE TO ONLY ALLOW CONTROL CHARS TO
  240.     SLIP BY, FOR MODALDIALOG TO PROCESS
  241.                     if ((theChar >= kPrintCharStart) && (theChar <= kPrintCharEnd)
  242.                             && (theChar != kDeleteKey))
  243. **************************************************************************************/
  244.                         // New check just for ctrl chars
  245.                     if (((unsigned short)theChar > kCtrlCharEnd) && (theChar != kDeleteKey))
  246.                     {
  247.                         iHit = ((DialogPeek)dialog)->editField + 1;
  248.                         GetDItem(dialog, iHit, &iType, &iHandle, &iRect);
  249.                         GetIText(iHandle, iString);
  250.                         if (iString[0] == kMaxCharLimit){
  251.                             // Exceeds Max Num Chars. Limit
  252.                             event->what = nullEvent;
  253.                             SysBeep(10);
  254.                         }
  255.                         if (event->what != nullEvent){
  256.                             theChar = (event->message & charCodeMask);
  257.                             /* Accept only lowercase chars or numbers,
  258.                                                 convert uppercase to lower */
  259.                             if (theChar < kSmall_a || theChar > kSmall_z){
  260.                                 if (theChar < kNumeralZero || theChar > kNumeralNine)
  261.                                 {
  262.                                         // Check to convert uppercase to lowercase
  263.                                     if (theChar >= kBig_A && theChar <= kBig_Z)
  264.                                         event->message += kUpperToLowerCaseOffset;
  265.                                     else{
  266.                                             // illegal char entered
  267.                                         event->what = nullEvent;
  268.                                         SysBeep(10);
  269.                                     }
  270.                                 }
  271.                             }
  272.                         }
  273.                     }
  274.                 }
  275.                 break;
  276.         }
  277.     }
  278.     return (returnVal);
  279. }
  280.  
  281.  
  282. /********************* buttonProc **************************/
  283.  
  284. pascal void        buttonProc( DialogPtr theDialog, short theItem)    // Outline Default Button
  285. {
  286.     short    type;
  287.     Rect    box;
  288.     Handle    itemHdl;
  289.  
  290.     GetDItem(theDialog, ok, &type, &itemHdl, &box);
  291.  
  292.     PenSize(3,3);
  293.     InsetRect( &box, -4, -4);
  294.     FrameRoundRect( &box, 16, 16);
  295.     PenSize(1,1);
  296. }